<!--
 * @Author: DUST
 * @Date: 2023-12-10 16:12:14
 * @LastEditors: DUST
 * @LastEditTime: 2023-12-14 09:45:43
 * @FilePath: \business-card-system\admin\src\components\tenMap.vue
 * @Description: 
 * undefined
-->
<template>
  <div id="mapContainer" style="width: 100%; height: 400px"></div>
</template>

<script lang="ts" setup>
import { onMounted, ref, watch, nextTick } from "vue";

const markerLayer = ref();
const map = ref();

onMounted(() => {
  const timer = setTimeout(() => {
    initMap(); // 渲染地图
    clearTimeout(timer);
  }, 1000);
});

const emit = defineEmits(["selectPosition"]);
const props = defineProps({
  lng: {
    type: Number,
    default: null,
  },
  lat: {
    type: Number,
    default: null,
  },
});
watch(
  () => props.lng,
  () => {
    if (props.lng) {
      console.log("有传入坐标", props.lng);
      nextTick(() => {
        const timer = setTimeout(() => {
          createMarker({
            lat: props.lat,
            lng: props.lng,
          });
          clearTimeout(timer);
        }, 1500);
      });
    }
  },
  {
    deep: true,
  }
);
const initMap = () => {
  // @ts-ignore
  const center = new TMap.LatLng(32.039912, 118.79886199999999); //设置地图中心点坐标
  // @ts-ignore
  map.value = new TMap.Map("mapContainer", {
    center: center,
    zoom: 18, //设置地图缩放级别
    baseMap: {
      type: "vector", //类型：失量底图
      // features: ["base", "building2d"],
    },
  });
  //监听点击事件添加marker
  map.value.on("click", (evt: any) => {
    console.log(evt);
    createMarker(evt.latLng);
  });
  // this.multiMarker();
  // this.infoWindow();
};
const createMarker = (position: { lng: number; lat: number }) => {
  if (markerLayer.value) {
    removeMarker();
  }
  //初始化marker图层
  // @ts-ignore
  markerLayer.value = new TMap.MultiMarker({
    map: map.value,
    // @ts-ignore
    geometries: [{ position: new TMap.LatLng(position.lat, position.lng) }],
  });
  //修改地图中心点
    // @ts-ignore
  map.value.panTo(new TMap.LatLng(position.lat, position.lng));
  emit("selectPosition", position);
  // 图标和气泡窗的显示 参考文章3.地图标点信息+图标
};
//移除marker事件
const removeMarker = () => {
  if (markerLayer.value) {
    markerLayer.value.setMap(null);
    markerLayer.value = null;
  }
};
</script>
